<!DOCTYPE html>
<html lang="zh_CN">
<head>
    <#include "../common/header.ftl" />
</head>
<body>

<div class="container " style="margin-top: 20px">
    <#include "../common/top.ftl" />
    <div class="row">
        <div class="col-sm-3">
        <#--声明变量-->
            <#assign menu="systemDictionaryItem"/>
            <#include "../common/menu.ftl" />
        </div>
        <div class="col-sm-9">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">字典管理</h1>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-4">
                    <ul id="menu" class="list-group">
                        <li class="list-group-item">
                            <a role="button" data-toggle="collapse" data-target="#system">
                                <span>字典目录列表</span>
                            </a>
                            <ul class="in" id="system">
                                <#list diction as d>
                                    <li data-id="${d.id}"><a href="javascript:;">${d.title}</a></li>
                                </#list>
                            </ul>
                        </li>
                    </ul>
                </div>
                <div class="col-sm-8">
                    <form class="form-inline" id="searchForm" action="" method="post">
                        <input type="hidden" id="currentPage" name="currentPage" value="1">
                    <#--添加目录id隐藏域-->
                        <input type="hidden" name="parentId" value="${(qo.parentId)!}">
                    </form>
                <#--如果有数据就显示-->
                 <#if pageInfo??>
                     <a class="btn btn-success inputBtn">
                         <span class="glyphicon glyphicon-plus"></span> 添加
                     </a>
                    <table class="table table-striped table-hover">
                        <tr>
                            <th>编号</th>
                            <th>明细名称</th>
                            <th>明细序号</th>
                            <th>操作</th>
                        </tr>
                        <#list pageInfo.list as entity>
                            <tr>
                                <td>${entity_index+1}</td>
                                <td>${entity.title}</td>
                                <td>${entity.sequence}</td>
                                <td>
                                    <a class="btn btn-info btn-xs inputBtn" data-json='${entity.json}'>
                                        <span class="glyphicon glyphicon-pencil"></span> 编辑
                                    </a>
                                    <a data-url="/systemDictionaryItem/delete.do?id=${entity.id}"
                                       class="btn btn-danger btn-xs deleteBtn">
                                        <span class="glyphicon glyphicon-trash"></span> 删除
                                    </a>
                                </td>
                            </tr>
                        </#list>
                    </table>
                     <#include "../common/page.ftl" />
                 </#if>
                </div>
            </div>
        </div>
    </div>
</div>
<#--字典编辑的模态框-->
<div class="modal fade" id="systemDictionaryItemModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
     aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
        <#--模态框的头部-->
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="systemDictionaryItemModal">编辑字典信息</h4>
            </div>
        <#--模态框的正文-->
            <div class="modal-body">
                <form class="form-horizontal" action="/systemDictionaryItem/saveOrUpdate.do" method="post"
                      id="editForm">
                    <input type="hidden" name="id">
                    <div class="form-group">
                        <label class="col-sm-4 control-label">字典目录：</label>
                        <div class="col-sm-6">
                        <#--给用户看-->
                            <input type="text" class="form-control" name="parentTitle" readonly>
                        <#--提交到后台的id-->
                            <input type="hidden" class="form-control" name="parentId">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-4 control-label">明细名称：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="title"
                                   placeholder="请输入明细名称">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-4 control-label">明细序号：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="sequence"
                                   placeholder="请输入明细序号">
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default glyphicon glyphicon-remove-sign" data-dismiss="modal">关闭
                </button>
                <button type="button" class="glyphicon glyphicon-ok-sign btn btn-primary saveBtn ">保存
                </button>
            </div>
        </div>
    </div>
</div>
<script>
    //关联自定义id
    $("#system li").click(function () {
        var id = $(this).data("id");
        $("#searchForm input[name=parentId]").val(id);
        $("#searchForm").submit();
    });
    //为选中的框添加active
    $("#system li[data-id='${qo.parentId}']").addClass("active");
    $(function () {

        $(".inputBtn").click(function () {
            //清空模态框
            $("#editForm input").val("");
            //拿到当前按钮的json数据
            var json = $(this).data("json");
            //只有具有json时才会回显数据
            if (json) {
                $("#editForm input[name='id']").val(json.id);
                //$("#editForm input[name='parentId']").val(json.parentId);
                $("#editForm input[name='title']").val(json.title);
                $("#editForm input[name='sequence']").val(json.sequence);
            }
            //获取选中的目录
            var li = $("#system li.active");
            //显示标题
            $("#editForm input[name=parentTitle]").val(li.text());
            $("#editForm input[name=parentId]").val(li.data("id"));

            $("#systemDictionaryItemModal").modal("show");
        });


        //异步提交数据
        $(".saveBtn").click(function () {
            $("#editForm").ajaxSubmit(function (data) {
                if (data.success) {
                    reload();
                }
            })
        })
        //删除数据
        $(".deleteBtn").click(function () {
            var url = $(this).data("url");
            $.messager.confirm("温馨提示", "删除后无法还原,是否确认删除", function () {
                $.get(url, function (data) {
                    if (data.success) {
                        reload();
                    } else {
                        $.messager.alert("温馨提示", data.msg);
                    }
                })
            })
        });
    })
</script>
</body>
</html>